package com.mezz.bones.framework.outbox.service;

import com.mezz.bones.framework.outbox.domain.entity.OutboxMessage;
import com.mezz.bones.framework.outbox.domain.entity.OutboxMessageArchive;
import com.mezz.bones.framework.outbox.repository.IOutboxMessageArchiveRepository;
import com.mezz.bones.framework.outbox.repository.IOutboxMessageRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class OutboxArchiveService {

    private final IOutboxMessageArchiveRepository outboxArchiveRepository;
    private final IOutboxMessageRepository outboxMessageRepository;

    /**
     * 归档消息
     */
    @Transactional(rollbackFor = Exception.class)
    public void archiveMessages(List<OutboxMessage> messages) {
        if (messages == null || messages.isEmpty()) {
            return;
        }

        List<OutboxMessageArchive> archives = new ArrayList<>(messages.size());
        List<Long> messageIds = new ArrayList<>(messages.size());
        Date now = new Date();

        for (OutboxMessage message : messages) {
            // 创建归档记录
            OutboxMessageArchive archive = new OutboxMessageArchive();
            archive.setMessageId(message.getMessageId());
            archive.setBusinessType(message.getBusinessType());
            archive.setBusinessKey(message.getBusinessKey());
            archive.setExecuteType(message.getExecuteType());
            archive.setExecuteTarget(message.getExecuteTarget());
            archive.setExecuteMethod(message.getExecuteMethod());
            archive.setExecuteParams(message.getExecuteParams());
            archive.setStatus(message.getStatus());
            archive.setRetryCount(message.getRetryCount());
            archive.setMaxRetry(message.getMaxRetry());
            archive.setNextRetryTime(message.getNextRetryTime());
            archive.setExecuteRetryPolicy(message.getExecuteRetryPolicy());
            archive.setCreateTime(message.getCreateTime());
            archive.setUpdateTime(message.getUpdateTime());
            archive.setEventTime(message.getEventTime());
            archive.setFirstExecuteTime(message.getFirstExecuteTime());
            archive.setLastExecuteTime(message.getLastExecuteTime());
            archive.setCompleteTime(message.getCompleteTime());
            archive.setLastExecuteResult(message.getLastExecuteResult());
            archive.setErrorStack(message.getErrorStack());
            archive.setExtParams(message.getExtParams());
            archive.setTags(message.getTags());
            archive.setPriority(message.getPriority());
            archive.setVersion(message.getVersion());
            archive.setArchiveTime(now);
            archive.setIsDel(message.getIsDel());
            archives.add(archive);
            messageIds.add(message.getId());
        }

        // 批量保存归档记录
        outboxArchiveRepository.saveBatch(archives);

        // 批量删除原消息
        outboxMessageRepository.removeByIds(messageIds);

        log.info("成功归档{}条消息", messages.size());
    }
}